import '../model/single_chat_message_model.dart';

/// 单聊消息默认数据
/// 围绕admin用户创建默认的单聊消息数据
class SingleChatMessageData {
  /// 获取默认单聊消息数据
  static List<SingleChatMessageModel> getDefaultSingleChatMessages() {
    final now = DateTime.now();

    return [
      // Admin与GardenMaster的聊天消息
      SingleChatMessageModel(
        id: 'msg_1',
        chatId: 'single_chat_1',
        senderId: '1', // admin
        content:
            'Hi! I saw your post about watering techniques. Could you share more details?',
        sendTime: now.subtract(const Duration(days: 5, hours: 2)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 5, hours: 1)),
      ),
      SingleChatMessageModel(
        id: 'msg_2',
        chatId: 'single_chat_1',
        senderId: '2', // gardenmaster
        content:
            'Of course! The key is to check soil moisture before watering. Stick your finger 1-2 inches deep.',
        sendTime: now.subtract(const Duration(days: 5, hours: 1, minutes: 30)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 5, hours: 1)),
      ),
      SingleChatMessageModel(
        id: 'msg_3',
        chatId: 'single_chat_1',
        senderId: '1', // admin
        content: 'That\'s very helpful! What about the frequency?',
        sendTime: now.subtract(const Duration(days: 5, hours: 1)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 5, minutes: 30)),
      ),
      SingleChatMessageModel(
        id: 'msg_4',
        chatId: 'single_chat_1',
        senderId: '2', // gardenmaster
        content:
            'It depends on the plant type and season. Generally once a week in summer, less in winter.',
        sendTime: now.subtract(const Duration(days: 5, minutes: 30)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 4, hours: 12)),
      ),
      SingleChatMessageModel(
        id: 'msg_5',
        chatId: 'single_chat_1',
        senderId: '2', // gardenmaster
        content: 'Thanks for the watering tips!',
        sendTime: now.subtract(const Duration(hours: 2)),
        isRead: false,
      ),

      // Admin与LeafyFriend的聊天消息
      SingleChatMessageModel(
        id: 'msg_6',
        chatId: 'single_chat_2',
        senderId: '3', // leafyfriend
        content:
            'Hello! I\'m new to plant care. Any recommendations for beginners?',
        sendTime: now.subtract(const Duration(days: 3, hours: 4)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 3, hours: 3)),
      ),
      SingleChatMessageModel(
        id: 'msg_7',
        chatId: 'single_chat_2',
        senderId: '1', // admin
        content:
            'Welcome! I\'d recommend starting with pothos or snake plants. They\'re very forgiving.',
        sendTime: now.subtract(const Duration(days: 3, hours: 3)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 3, hours: 2)),
      ),
      SingleChatMessageModel(
        id: 'msg_8',
        chatId: 'single_chat_2',
        senderId: '3', // leafyfriend
        content: 'Great! I just bought a pothos. Any care tips?',
        sendTime: now.subtract(const Duration(days: 3, hours: 2)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 3, hours: 1)),
      ),
      SingleChatMessageModel(
        id: 'msg_9',
        chatId: 'single_chat_2',
        senderId: '1', // admin
        content: 'How is your new plant doing?',
        sendTime: now.subtract(const Duration(minutes: 30)),
        isRead: false,
      ),

      // Admin与PlantCare的聊天消息
      SingleChatMessageModel(
        id: 'msg_10',
        chatId: 'single_chat_3',
        senderId: '4', // plantcare
        content:
            'Hi admin! I have a question about my fiddle leaf fig. The leaves are turning brown.',
        sendTime: now.subtract(const Duration(days: 7, hours: 3)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 7, hours: 2)),
      ),
      SingleChatMessageModel(
        id: 'msg_11',
        chatId: 'single_chat_3',
        senderId: '1', // admin
        content:
            'Brown leaves could be due to overwatering or low humidity. How often do you water it?',
        sendTime: now.subtract(const Duration(days: 7, hours: 2)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 7, hours: 1)),
      ),
      SingleChatMessageModel(
        id: 'msg_12',
        chatId: 'single_chat_3',
        senderId: '4', // plantcare
        content: 'I water it twice a week. Should I reduce it?',
        sendTime: now.subtract(const Duration(days: 7, hours: 1)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 6, hours: 12)),
      ),
      SingleChatMessageModel(
        id: 'msg_13',
        chatId: 'single_chat_3',
        senderId: '1', // admin
        content:
            'Yes, try once a week and check the soil first. Also consider misting the leaves.',
        sendTime: now.subtract(const Duration(days: 6, hours: 12)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 6, hours: 6)),
      ),
      SingleChatMessageModel(
        id: 'msg_14',
        chatId: 'single_chat_3',
        senderId: '4', // plantcare
        content: 'I\'ll send you some photos tomorrow',
        sendTime: now.subtract(const Duration(hours: 1)),
        isRead: false,
      ),

      // Admin与GreenSoul的聊天消息
      SingleChatMessageModel(
        id: 'msg_15',
        chatId: 'single_chat_4',
        senderId: '5', // greensoul
        content:
            'Hey! I tried the fertilizer you recommended. My plants look amazing!',
        sendTime: now.subtract(const Duration(days: 2, hours: 5)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 2, hours: 4)),
      ),
      SingleChatMessageModel(
        id: 'msg_16',
        chatId: 'single_chat_4',
        senderId: '1', // admin
        content: 'That\'s fantastic! Which fertilizer did you use?',
        sendTime: now.subtract(const Duration(days: 2, hours: 4)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 2, hours: 3)),
      ),
      SingleChatMessageModel(
        id: 'msg_17',
        chatId: 'single_chat_4',
        senderId: '5', // greensoul
        content:
            'The organic liquid fertilizer. My monstera has grown 3 new leaves!',
        sendTime: now.subtract(const Duration(days: 2, hours: 3)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 1, hours: 12)),
      ),
      SingleChatMessageModel(
        id: 'msg_18',
        chatId: 'single_chat_4',
        senderId: '5', // greensoul
        content: 'The fertilizer worked great!',
        sendTime: now.subtract(const Duration(days: 1)),
        isRead: false,
      ),

      // Admin与Blooming的聊天消息
      SingleChatMessageModel(
        id: 'msg_19',
        chatId: 'single_chat_5',
        senderId: '6', // blooming
        content: 'Hi admin! My roses are not blooming well. Any advice?',
        sendTime: now.subtract(const Duration(days: 4, hours: 6)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 4, hours: 5)),
      ),
      SingleChatMessageModel(
        id: 'msg_20',
        chatId: 'single_chat_5',
        senderId: '1', // admin
        content:
            'Roses need at least 6 hours of direct sunlight and regular pruning. Are they getting enough sun?',
        sendTime: now.subtract(const Duration(days: 4, hours: 5)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 4, hours: 4)),
      ),
      SingleChatMessageModel(
        id: 'msg_21',
        chatId: 'single_chat_5',
        senderId: '6', // blooming
        content: 'They get about 4 hours. Should I move them?',
        sendTime: now.subtract(const Duration(days: 4, hours: 4)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 4, hours: 3)),
      ),
      SingleChatMessageModel(
        id: 'msg_22',
        chatId: 'single_chat_5',
        senderId: '1', // admin
        content:
            'Yes, try a sunnier spot. Also, make sure to deadhead spent blooms regularly.',
        sendTime: now.subtract(const Duration(days: 4, hours: 3)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 3, hours: 12)),
      ),
      SingleChatMessageModel(
        id: 'msg_23',
        chatId: 'single_chat_5',
        senderId: '1', // admin
        content: 'Can you help me with pruning?',
        sendTime: now.subtract(const Duration(hours: 6)),
        isRead: false,
      ),

      // Admin与PlantLover的聊天消息
      SingleChatMessageModel(
        id: 'msg_24',
        chatId: 'single_chat_6',
        senderId: '7', // plantlover
        content: 'Hello! Are you free this weekend for the plant swap event?',
        sendTime: now.subtract(const Duration(days: 1, hours: 8)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 1, hours: 7)),
      ),
      SingleChatMessageModel(
        id: 'msg_25',
        chatId: 'single_chat_6',
        senderId: '1', // admin
        content: 'Yes! What time does it start?',
        sendTime: now.subtract(const Duration(days: 1, hours: 7)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 1, hours: 6)),
      ),
      SingleChatMessageModel(
        id: 'msg_26',
        chatId: 'single_chat_6',
        senderId: '7', // plantlover
        content:
            'It starts at 2 PM at the community center. I\'ll bring some cuttings to share!',
        sendTime: now.subtract(const Duration(days: 1, hours: 6)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 1, hours: 5)),
      ),
      SingleChatMessageModel(
        id: 'msg_27',
        chatId: 'single_chat_6',
        senderId: '1', // admin
        content: 'Perfect! I\'ll bring some of my pothos cuttings too.',
        sendTime: now.subtract(const Duration(days: 1, hours: 5)),
        isRead: true,
        readTime: now.subtract(const Duration(days: 1, hours: 4)),
      ),
      SingleChatMessageModel(
        id: 'msg_28',
        chatId: 'single_chat_6',
        senderId: '7', // plantlover
        content: 'Sure, I\'ll be there at 3 PM',
        sendTime: now.subtract(const Duration(minutes: 15)),
        isRead: false,
      ),

      // Admin与GardenNewbie的聊天消息
      SingleChatMessageModel(
        id: 'msg_29',
        chatId: 'single_chat_7',
        senderId: '8', // gardennewbie
        content: 'Hi! I\'m completely new to gardening. Where should I start?',
        sendTime: now.subtract(const Duration(hours: 12, minutes: 30)),
        isRead: true,
        readTime: now.subtract(const Duration(hours: 12)),
      ),
      SingleChatMessageModel(
        id: 'msg_30',
        chatId: 'single_chat_7',
        senderId: '1', // admin
        content:
            'Welcome to our plant community! I\'d suggest starting with easy plants like succulents or herbs.',
        sendTime: now.subtract(const Duration(hours: 12)),
        isRead: true,
        readTime: now.subtract(const Duration(hours: 11)),
      ),
      SingleChatMessageModel(
        id: 'msg_31',
        chatId: 'single_chat_7',
        senderId: '8', // gardennewbie
        content: 'What about basil? I love cooking with fresh herbs.',
        sendTime: now.subtract(const Duration(hours: 11)),
        isRead: true,
        readTime: now.subtract(const Duration(hours: 10)),
      ),
      SingleChatMessageModel(
        id: 'msg_32',
        chatId: 'single_chat_7',
        senderId: '1', // admin
        content:
            'Basil is perfect for beginners! It needs lots of sun and regular watering. Start with seeds or a small plant.',
        sendTime: now.subtract(const Duration(hours: 10)),
        isRead: true,
        readTime: now.subtract(const Duration(hours: 9)),
      ),
      SingleChatMessageModel(
        id: 'msg_33',
        chatId: 'single_chat_7',
        senderId: '8', // gardennewbie
        content: 'Great! I\'ll try that. Thanks for the advice!',
        sendTime: now.subtract(const Duration(hours: 9)),
        isRead: true,
        readTime: now.subtract(const Duration(hours: 8)),
      ),
      SingleChatMessageModel(
        id: 'msg_34',
        chatId: 'single_chat_7',
        senderId: '1', // admin
        content: 'Welcome to our plant community!',
        sendTime: now.subtract(const Duration(minutes: 5)),
        isRead: false,
      ),
    ];
  }

  /// 获取指定聊天的消息列表
  static List<SingleChatMessageModel> getMessagesByChatId(String chatId) {
    return getDefaultSingleChatMessages()
        .where((message) => message.chatId == chatId)
        .toList()
      ..sort((a, b) => a.sendTime.compareTo(b.sendTime));
  }

  /// 获取指定用户的消息列表
  static List<SingleChatMessageModel> getMessagesByUserId(String userId) {
    return getDefaultSingleChatMessages()
        .where((message) => message.senderId == userId)
        .toList()
      ..sort((a, b) => b.sendTime.compareTo(a.sendTime));
  }

  /// 获取未读消息数量
  static int getUnreadCount(String currentUserId) {
    return getDefaultSingleChatMessages()
        .where(
          (message) => message.senderId != currentUserId && !message.isRead,
        )
        .length;
  }

  /// 获取指定聊天的未读消息数量
  static int getChatUnreadCount(String chatId, String currentUserId) {
    return getDefaultSingleChatMessages()
        .where(
          (message) =>
              message.chatId == chatId &&
              message.senderId != currentUserId &&
              !message.isRead,
        )
        .length;
  }

  /// 获取消息统计信息
  static Map<String, dynamic> getMessageStatistics() {
    final messages = getDefaultSingleChatMessages();
    final stats = <String, dynamic>{};

    // 总消息数
    stats['totalMessages'] = messages.length;

    // 已读消息数
    stats['readMessages'] = messages.where((msg) => msg.isRead).length;

    // 未读消息数
    stats['unreadMessages'] = messages.where((msg) => !msg.isRead).length;

    // 按聊天统计消息数
    final chatMessageCounts = <String, int>{};
    for (final message in messages) {
      chatMessageCounts[message.chatId] =
          (chatMessageCounts[message.chatId] ?? 0) + 1;
    }
    stats['chatMessageCounts'] = chatMessageCounts;

    // 按用户统计消息数
    final userMessageCounts = <String, int>{};
    for (final message in messages) {
      userMessageCounts[message.senderId] =
          (userMessageCounts[message.senderId] ?? 0) + 1;
    }
    stats['userMessageCounts'] = userMessageCounts;

    return stats;
  }

  /// 获取最新消息
  static List<SingleChatMessageModel> getLatestMessages({int limit = 10}) {
    final messages = getDefaultSingleChatMessages();
    messages.sort((a, b) => b.sendTime.compareTo(a.sendTime));
    return messages.take(limit).toList();
  }

  /// 搜索消息
  static List<SingleChatMessageModel> searchMessages(String keyword) {
    if (keyword.isEmpty) return [];

    return getDefaultSingleChatMessages()
        .where(
          (message) =>
              message.content.toLowerCase().contains(keyword.toLowerCase()),
        )
        .toList()
      ..sort((a, b) => b.sendTime.compareTo(a.sendTime));
  }
}
